package com.fwtai.quartz;

import com.fwtai.service.SpringContextHolder;
import com.fwtai.service.api.ApiGoodsOrderService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * 定义`自动发货步骤`,处理3天后订单自动改为已收货
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2024/1/29 14:58
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Component
@EnableScheduling
public class AutoDelivery implements Job{

  public final static String groupName = AutoDelivery.class.getName();

  public final static String dateKey = "autoKid";//数据key的标识

  @Override
  public void execute(final JobExecutionContext context){
    final JobDetail jobDetail = context.getJobDetail();
    final JobDataMap jobDataMap = jobDetail.getJobDataMap();
    final String values = jobDataMap.getString(dateKey);
    final String[] split = values.split(",");// 存入的值是 outTradeNo+","+type+","+transactionId
    final long outTradeNo = Long.parseLong(split[0]);
    final String type = split[1];
    switch (type){
      case "1"://判断微信是否已自动收货,步骤1.需修改本系统的订单状态;步骤2.查询微信服务器的订单状态是否已确认收货
        ToolQuartz.editDelayedResult(split[2],outTradeNo,type);//自动发货步骤2:到时执行任务`微信支付订单`，查询微信服务器的发货状态,最终调用 editDelivery
        break;
      case "2"://仅需修改本系统的订单状态即可
        final int rows = SpringContextHolder.getBean(ApiGoodsOrderService.class).editDelivery(outTradeNo,type);//1 或 2,自动发货步骤2:到时执行任务`积分支付订单`
        if(rows > 0){
          ToolQuartz.deleteJob(String.valueOf(outTradeNo),groupName);
        }
        break;
      default:
        break;
    }
  }
}